import router from './router'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/sign'] // 路由白名单

router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start()
  // determine whether the user has logged in
  // const hasToken = window.localStorage.getItem('token')
  const hasToken = true //后期需要添加判断
  /*
  * to do hasToken
  * */

  if (hasToken) {
    // 登录的情况下
    next()
  } else {
    //没有登录的情况下
    if (whiteList.indexOf(to.path) !== -1) {
      // 白名单里面的路由直接访问
      next()
    } else {
      // 不是路由白名单里的就需要进行登录
      next(`/sign?redirect=${to.path}`)
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
